package adapter;

import java.util.ArrayList;
import java.util.TreeSet;

import db.AdminBaseDatosHelper;
import lfm.mpr.R;
import lfm.mpr.TeamTabsActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import objects.Equipo;
import objects.Jugador;

public class AdapterListEquipoDetalle extends ArrayAdapter<Jugador>{

	private ArrayList<Jugador> list_jugadores;
	private Context context;
	private AdminBaseDatosHelper miBBDDHelper;
	
	private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	public AdapterListEquipoDetalle(Context context, ArrayList<Jugador> list) {
		super(context, R.layout.list_view_equipo, list);
		this.context = context;
        this.list_jugadores = list;
        miBBDDHelper = new AdminBaseDatosHelper(this.context);
        
	}//end
	
	static class ViewHolder {
        protected TextView nombre, precio, equipo, posicion, remove_jugador;
        protected CheckBox checkbox;
        protected ImageView img;
    }
	  
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
        final ViewHolder viewHolder;
        
        if (convertView == null) {
        	
        	viewHolder = new ViewHolder();
        	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	
	        convertView  = inflater.inflate(R.layout.list_view_equipo, null);
	            
	        viewHolder.nombre = (TextView) convertView.findViewById(R.id.text_lst_name_team);
	        viewHolder.precio = (TextView) convertView.findViewById(R.id.text_lst_price_team);
	        viewHolder.equipo = (TextView) convertView.findViewById(R.id.text_lst_team_team);
	        viewHolder.posicion = (TextView) convertView.findViewById(R.id.textView_jugador_posicion_team);
	        viewHolder.remove_jugador = (TextView) convertView.findViewById(R.id.check_team);
	        viewHolder.img = (ImageView)  convertView.findViewById(R.id.foto_jugador_team);
	       
            convertView.setTag(viewHolder);

        } else {
        	viewHolder = (ViewHolder) convertView.getTag();
        }
        
       
	        viewHolder.nombre.setText(this.list_jugadores.get(position).getNombre());
	        viewHolder.precio.setText(" " + String.valueOf(this.list_jugadores.get(position).getPrecio()));
	        viewHolder.equipo.setText(this.list_jugadores.get(position).getEquipo());
	        String posicion = this.list_jugadores.get(position).getPosicion().substring(0, 1).toUpperCase() + 
	        			this.list_jugadores.get(position).getPosicion().substring(1).toLowerCase();
	        viewHolder.posicion.setText(" " + posicion);
	        
	        String uri = "";
	        if(list_jugadores.get(position).getPosicion().equals("portero")) uri = "drawable/" + "p_" + String.valueOf(list_jugadores.get(position).getCodigo());
	        	else if(list_jugadores.get(position).getPosicion().equals("defensa")) uri = "drawable/" + "def_" + String.valueOf(list_jugadores.get(position).getCodigo());
	        		else if(list_jugadores.get(position).getPosicion().equals("medio")) uri = "drawable/" + "m_" + String.valueOf(list_jugadores.get(position).getCodigo());
	        			else  uri = "drawable/" + "del_" + String.valueOf(list_jugadores.get(position).getCodigo());
	        
	        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
	        Drawable img1;
	        if(imageResource == 0) img1 = context.getResources().getDrawable(R.drawable.ic_launcher);
	        	else img1 = context.getResources().getDrawable(imageResource);
	        viewHolder.img.setImageDrawable(img1);
		       
   	

        return convertView;
        }
	
    }//end_class
